package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import DB.DB_connection_pool;


public class leaving_messages_user extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String user_account = request.getSession().getAttribute("user_account").toString();
		JSONObject jsonObj_r = new JSONObject();
		String select_sql = "select distinct send_leaving_messages from leaving_messages where receive_leaving_messages = '"+user_account+"'";
		String select_leaving_user = "select * from user_data where user_account in ("+select_sql+")";
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select_leaving_user = conn.prepareStatement(select_leaving_user);
			ResultSet rs_leaving_user = ps_select_leaving_user.executeQuery();

			JSONArray my_leaving_user = new JSONArray();
			while(rs_leaving_user.next()){
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("user_account", rs_leaving_user.getString("user_account"));
				jsonObj.put("user_nickname", rs_leaving_user.getString("user_nickname"));
				jsonObj.put("user_photo_path", rs_leaving_user.getString("user_photo_path"));
				my_leaving_user.put(jsonObj);
			}
			
			jsonObj_r.put("result", true);
			jsonObj_r.put("my_leaving_user", my_leaving_user);
		} catch (SQLException e) {
			jsonObj_r.put("result", false);
			e.printStackTrace();
		}
		
		out.print(jsonObj_r.toString());
		
	}

}
